(function() {
    // getting a reference to the main namespace if it exists - empty object otherwise
    var Prestatheme = window.Prestatheme = window.Prestatheme || {};
    
    Prestatheme.runDebug = function (msg)
    {
        if (Prestatheme.DEBUG && Prestatheme.StepToStep)
        {
            alert (msg);
        }
    };
    
    /*
     * Fonction d'initialisation des variables globales
     */
    Prestatheme.initialize = function() 
    {
        Prestatheme.hasAjaxReq = false;
        Prestatheme.DEBUG      = true;
        Prestatheme.StepToStep = false;
        Prestatheme.updated    = false;
        try
        {
            Prestatheme.runDebug ("[INIT][initialize] Initialize listeners");
            
			//	Get default css style
			Prestatheme.configs   = $.parseJSON(stylesJSON, false);
            Prestatheme.styles    = new Array();
			Prestatheme.designCfg = $.parseJSON(designJSON, false);
			Prestatheme.baseurl   = $("meta[name=baseUrl]").attr('content') + 'index.php?ajax=1';
			Prestatheme.ajaxAsyn  = false;
			
			// The number of running ajax request
			Prestatheme.process = 0;
			
			// Initialize the data structure saving the css
			Prestatheme.initDefaultStyle();
			
			// launch listerners
			Prestatheme.initListerners();
			
			//apply default style
			$('iframe#previewer').ready(function () {
				Prestatheme.refresh();
			});
			
			// Start the deamon
			var timer = setInterval(Prestatheme.deamon, 10000);
        }
        catch (e)
        {
            Prestatheme.manageError ('INI', e);
        }
    };
	
    Prestatheme.initDefaultStyle = function() 
    {
        try
        {
            Prestatheme.runDebug ("[INIT][initDefaultStyle] Initialize the liste of styles");
            for (var i=0; i<Prestatheme.configs.length; i++)
			{
				item = Prestatheme.configs[i];
                
                // apply the css style
				if (item.fullvalue)
				{
					var stylecss = item.fullvalue.split(';');
					for (var j=0; j<stylecss.length; j++)
					{
						style = stylecss[j];
						style = style.split(':');
						
						if (style.length != 2) break;
						
						Prestatheme.updateStyle(item.selector, style[0], style[1]);
					}
				}
			}
        }
        catch (e)
        {
            Prestatheme.manageError ('IND', e);
        }
    };
	
    Prestatheme.initListerners = function() 
    {
        try
        {
            Prestatheme.runDebug ("[INIT][initListerners] Initialize the listerners");
			
			// On choose design
			$('.choose_design').click (function (e) {
				Prestatheme.onChooseDesign('#'+ e.target.id);
			});
        }
        catch (e)
        {
            Prestatheme.manageError ('INL', e);
        }
    };
    
    /* Manage error */
    Prestatheme.manageError = function(code, error)
    {
        Prestatheme.runDebug ("[ALERT][manageError] An error occured. code: "+ code);
        
		if (Prestatheme.DEBUG)
		{
			switch (code)
			{
				case 'UNK' : //Unknow error
				default    :
					alert('['+ code +'] An error occured. Message: ' + error);
					break;
			}
		}
    };
})();

$(document).ready(function () {
    Prestatheme.initialize();
});